//
//  GRJSDWebimageViewController.m
//  GRJTolCayFrwk
//
//  Created by 易上云 on 2017/3/13.
//  Copyright © 2017年 Global Barter. All rights reserved.
//

#import "GRJSDWebimageViewController.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"

#import "SDWebImageManager.h"

@interface GRJSDWebimageViewController ()
@property(nonatomic,strong)UIImageView *imageV;

@end

@implementation GRJSDWebimageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"SDWebimage";
    
    [self setUpImageView];
    
}

-(void)setUpImageView{
    UIImageView *imageV =[[UIImageView alloc]init];
    self.imageV = imageV;
    
    [self.view addSubview:imageV];
    
    imageV.image = [UIImage imageNamed:@"QQ"];
    
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(100);
        make.left.equalTo(self.view.mas_left).offset(10);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.height.equalTo(@200);
    }];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"加载" style:UIBarButtonItemStylePlain target:self action:@selector(download)];
}

-(void)download{
    NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489385848227&di=2d75cd90c5c19f19cd820071e3d8bb1f&imgtype=0&src=http%3A%2F%2Ftupian.enterdesk.com%2F2013%2Fxll%2F011%2F05%2F3%2F13.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    self.imageV.image = image;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1489385848226&di=58d5fe1708d60d1c848d0c4d84e318bd&imgtype=0&src=http%3A%2F%2Fimg02.tooopen.com%2Fimages%2F20150601%2Ftooopen_sy_127964478185.jpg"];
    
    //[self.imageV sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"yingzhiren"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        NSLog(@"加载图片完毕");
//    }];
    
    [self.imageV sd_setImageWithPreviousCachedImageWithURL:url placeholderImage:[UIImage imageNamed:@"yingzhiren"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
         NSLog(@"%f",1.0 * receivedSize / expectedSize);
        
    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"加载图片完毕");
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
